/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bash.jcouture.util.swing;


import java.awt.Component;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import jxl.*;

/**
 *
 * @author bash
 */
public class FileHandler {

    public static File chooseFile(Component parent,FileFilter filter) {
        JFileChooser chooser = new JFileChooser();
         chooser.setFileFilter((javax.swing.filechooser.FileFilter) filter);

        int returnVal = chooser.showOpenDialog(parent);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            return chooser.getSelectedFile();
        }

        return null;

    }
    
    public static File getFileToSaveIn(Component parent,FileFilter filter){
        JFileChooser chooser = new JFileChooser();
      chooser.setFileFilter((javax.swing.filechooser.FileFilter) filter);
        File file = null;

        int returnVal = chooser.showSaveDialog(parent);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file= chooser.getSelectedFile();
        }

        return file;
    }

    public static Sheet readExcelSheet(File file) {
        // ouverture du fichier (workbook) pour la lecture
        Workbook wbk;
        try {
            wbk = Workbook.getWorkbook(file);
        } catch (Exception ex) {
            JOptionPane message = new JOptionPane(
                    "Impossible de lire le fichier spécifié." + file.getPath(),
                    JOptionPane.ERROR_MESSAGE);
            return null;
        }
        if (wbk.getNumberOfSheets() <= 0) {
            JOptionPane message = new JOptionPane(
                    "Ce fichier Excel n'a aucune feuille",
                    JOptionPane.ERROR_MESSAGE);
            return null;
        }
        // returner la premiere feuille
        return wbk.getSheet(0);
    }
    
    
  
    
}
